rm(list=ls())

set.seed(1234)

library(plyr)
library(ggplot2)
library(car)
library(psych)
library(texreg)
library(effectsize)

setwd(".../Data/Raw")

Port<-read.csv('PortugalComplete.csv')


modeldata<-Port[,c(7, 19, 20, 38, 39, 41, 43, 45, 47:54, 64:76, 77, 83, 92:94)] #subset to variables needed for models 

modeldata<-subset(modeldata, Duration..in.seconds. > 300) # remove speedsters, those that complete the survey in less than 300 seconds 

#Recode outcome variables --> higher values == fairer / stronger agreement 

modeldata$decide_citizen <-as.character(modeldata$decide_citizen)
modeldata$decide_women <-as.character(modeldata$decide_women)
modeldata$decide_animal <-as.character(modeldata$decide_animal)
modeldata$fair_women <-as.character(modeldata$fair_women)
modeldata$fair_animal <-as.character(modeldata$fair_animal)
modeldata$decisionprocess <-as.character(modeldata$decisionprocess)
modeldata$overturn <-as.character(modeldata$overturn)
modeldata$council_trust <-as.character(modeldata$council_trust)
modeldata$personalcouncil <-as.character(modeldata$personalcouncil)

modeldata[modeldata =="Muy injusto"]<-1
modeldata[modeldata =="Algo injusto"]<-2
modeldata[modeldata =="Algo justo"]<-3
modeldata[modeldata =="Muy justo"]<-4

modeldata[modeldata =="Muito injusta"]<-1
modeldata[modeldata =="Um pouco injusta"]<-2
modeldata[modeldata =="Um pouco justa"]<-3
modeldata[modeldata =="Um pouca justa"]<-3
modeldata[modeldata =="Muito justa"]<-4

modeldata[modeldata =="Muito injusto"]<-1
modeldata[modeldata =="Um pouco injusto"]<-2
modeldata[modeldata =="Um pouco justo"]<-3
modeldata[modeldata =="Um pouca justo"]<-3
modeldata[modeldata =="Muito justo"]<-4

modeldata[modeldata =="Discordo totalmente"]<-1
modeldata[modeldata =="Discordo"]<-2
modeldata[modeldata =="Concordo"]<-3
modeldata[modeldata =="Concordo totalmente"]<-4

modeldata$decide_citizen<-as.numeric(as.character(modeldata$decide_citizen))
modeldata$decide_women <-as.numeric(as.character(modeldata$decide_women))
modeldata$decide_animal <-as.numeric(as.character(modeldata$decide_animal))
modeldata$fair_women <-as.numeric(as.character(modeldata$fair_women))
modeldata$fair_animal <-as.numeric(as.character(modeldata$fair_animal))
modeldata$decisionprocess <-as.numeric(as.character(modeldata$decisionprocess))
modeldata$overturn <-as.numeric(as.character(modeldata$overturn))
modeldata$council_trust <-as.numeric(as.character(modeldata$council_trust))
modeldata$personalcouncil <-as.numeric(as.character(modeldata$personalcouncil))

#reverse code overturn, so higher values represent higher legitimacy views

modeldata$overturnR<-recode(modeldata$overturn, " 1=4; 2=3; 3=2; 4=1")

##sexual harassment vignettes (treat 1 = AMP, treat 2 = GBP, treat 3 = Q-GBP)

harass<-subset(modeldata, Vignettes_DO == "treat1" | Vignettes_DO == "treat2" | Vignettes_DO == "treat3")

#Create indicies for three treatment groups 

#substantive legitimacy
factor1<-omega(harass[,c(17, 18, 20)], nfactors=1) 
harass$SubLeg<-factor1$scores[,1]
harass$SubLegStand<-standardize(harass$SubLeg)

#t-tests for procedural legit
factor1<-omega(harass[,c(22, 35, 24)], nfactors=1) 
harass$ProLeg<-factor1$scores[,1]
harass$ProLegStand<-standardize(harass$ProLeg)

harass <- harass[!is.na(harass$SubLegStand),] #remove NA values from DV
harass <- harass[!is.na(harass$ProLegStand),] #remove NA values from DV

##animal mistreatment vignettes 

animal<-subset(modeldata, Vignettes_DO == "treat4" | Vignettes_DO == "treat5" | Vignettes_DO == "treat6")

#Create indicies and plot for three treatment groups 
#substantive legitimacy 

factor1<-omega(animal[,c(17,19, 21)], nfactors=1) 
animal $SubLeg<-factor1$scores[,1]
animal $SubLegStand<-standardize(animal $SubLeg)

#procedural legitimacy 

factor1<-omega(animal[,c(22, 35, 24)], nfactors=1) #alpha = 0.83
animal $ProLeg<-factor1$scores[,1]
animal $ProLegStand<-standardize(animal $ProLeg)

animal <- animal[!is.na(animal $SubLegStand),]
animal <- animal[!is.na(animal $ProLegStand),]

#create and export model dataset 

PortAgg<-rbind(harass[, c(33, 37, 39, 34, 2)], animal[, c(33, 37, 39, 34, 2)]) 
PortAgg $Country <-rep("Portugal", nrow(PortAgg))

write.csv(PortAgg, "PortAgg.csv")

